<?php
/**
 * Notification util
 *
 * PHP version 5
 *
 * @category   Web
 * @package    Tourstart
 * @subpackage Util
 * @author     Alex Slubsky <aslubsky@gmail.com>
 * @license    Tourstart License http://tourstart.dk/license
 * @version    SVN: $Id: $
 *
 *
 */

/**
 * Includes
 */


/**
 * Notification util
 *
 * PHP version 5
 *
 * @category   Web
 * @package    Tourstart
 * @subpackage Search
 * @author     Alex Slubsky <aslubsky@gmail.com>
 * @license    Tourstart License http://tourstart.dk/license
 * @version    Release: $Id: $
 *
 *
 */

class DaemonParseDoc extends DaemonAbstract
{
    protected static function get_memory_usage()
    {
        $mem_usage = memory_get_usage(true);

        if ($mem_usage < 1024)
            return $mem_usage . ' bytes';
        elseif ($mem_usage < 1048576)
            return round($mem_usage/1024,2) . ' kilobytes';
        else
            return round($mem_usage/1048576,2) . ' megabytes';

        return $mem_usage;
    }

    public function scanDocs($folder)
    {
        $this->logger->debug('Start scaning ' . $folder);

        foreach (glob($folder . '/*', GLOB_ONLYDIR) as $dir) {
            $this->scanDocs($dir);
        }

        foreach (glob($folder . '/*.xml') as $fileName) {
            DocumentationParser::parseFile(1, $fileName);
            $this->logger->debug('memory usage: ' . self::get_memory_usage());
        }
    }

    public function scanTests($folder)
    {
        $this->logger->debug('Start 2222 scaning ' . $folder);
        $files = CoverageUtils::parseCoverageFiles( $folder );
        foreach ($files as $k => $info) {
            $file = Pfile::getFile($k);
            $this->logger->debug('save CoverageFile - '.$k.' / '.$file->id);
            CoverageFile::saveInfo($file, $info);
            $this->logger->debug('memory usage: ' . self::get_memory_usage());
        }    
    }
    
    public function work()
    {
        using('Site.Code.Documentation');
        using('Site.Code.Coverage');

        $this->logger->debug('Start parsing documents');
        $this->logger->debug('memory usage: ' . self::get_memory_usage());

        $done = Scheduler::getNotParsed();
        $this->logger->debug('Find ' . count($done) . ' task(s)');

        foreach ($done as $task) {        
            $task->state = Scheduler::START_TASK;
            $task->save();
            
            $project = $task->Project;
            Pfolder::deleteByProject($project);
            
            $this->scanDocs( $project->getDocsDir() );
            $this->scanTests( $project->getCoverageDir() );
            
            $updTask = Scheduler::getById(intval($task->id));
            if($updTask->state == Scheduler::CANCEL_TASK) {
                $updTask->delete();
            } else {
                $task->state = Scheduler::SUCCESS_TASK;
                $task->parsed = 1;
                $task->save();
            }
        }
		if(count($done) > 0) {
			$this->needRestart = true;
		}
        $this->logger->debug('Parsing complete');
        $this->logger->debug('memory usage: ' . self::get_memory_usage());		
    }
}


